//
//  OALoginView.m
//  LongOA
//
//  Created by 耿用强 on 15/7/9.
//  Copyright (c) 2015年 耿用强. All rights reserved.
//

#import "OALoginView.h"

@implementation OALoginView
{
    UIButton*_oquen;
    UIImageView*_blackView;
    UIView*_topView;
    UILabel*_topLabel;
    UITextField*_textField1;
    UITextField*_textField2;
    UIButton*_cancelBt;
    UIButton*_commintBt;
    
    UIView*_line1;
    UIView*_line2;
}
- (id)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        
        _oquen=[UIButton buttonWithType:UIButtonTypeCustom];
        _oquen.backgroundColor=[UIColor colorWithWhite:0.1 alpha:0.35];
        [_oquen addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_oquen];
        
        
        _blackView=[[UIImageView alloc]init];
        _blackView.userInteractionEnabled=YES;
        _blackView.backgroundColor=[UIColor whiteColor];
        [self addSubview:_blackView];
        
        
        _topView=[[UIView alloc]init];
        _topView.backgroundColor=BLUE_COLOR;
        [_blackView addSubview:_topView];
        
        _topLabel=[[UILabel alloc]init];
        _topLabel.backgroundColor=[UIColor clearColor];
        _topLabel.textColor=[UIColor whiteColor];
        _topLabel.font=FONT_BOLD_17;
        _topLabel.text=@"请输入账户名和密码";
        [_topView addSubview:_topLabel];
        

 
    _textField1=[[UITextField alloc]init];
    _textField1.delegate=self;
    _textField1.placeholder=@"用户账户";
    _textField1.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textField1.backgroundColor=[UIColor clearColor];
    _textField1.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    _textField1.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        _textField1.returnKeyType=UIReturnKeyDone;
    _textField1.font=FONT_BOLD_14;
    _textField1.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
    _textField1.textColor=[UIColor darkGrayColor];
        //2015.09.13
        //_textField1.text=@"001";
    [_blackView addSubview:_textField1];
        _line1=[[UIView alloc]init];
        _line1.backgroundColor=BLUE_COLOR;
        [_blackView addSubview:_line1];
        _textField2=[[UITextField alloc]init];
        _textField2.delegate=self;
        _textField2.returnKeyType=UIReturnKeyDone;
        _textField2.placeholder=@"请输入密码";
        _textField2.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField2.backgroundColor=[UIColor clearColor];
        
        _textField2.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
        _textField2.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        _textField2.font=FONT_BOLD_14;
        _textField2.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
        _textField2.textColor=[UIColor darkGrayColor];
        [_blackView addSubview:_textField2];
        
        //2015.09.13
        //_textField2.text=@"001";
        
        _line2=[[UIView alloc]init];
        _line2.backgroundColor=[UIColor grayColor];
        [_blackView addSubview:_line2];

        _cancelBt=[UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBt addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
        [_cancelBt setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBt setTitleColor:BLUE_COLOR forState:UIControlStateNormal];
        _cancelBt.titleLabel.font=FONT_16;
        [_blackView addSubview:_cancelBt];
        
        _commintBt=[UIButton buttonWithType:UIButtonTypeCustom];
        [_commintBt addTarget:self action:@selector(doLogin) forControlEvents:UIControlEventTouchUpInside];
        [_commintBt setTitle:@"登录" forState:UIControlStateNormal];
        [_commintBt setTitleColor:BLUE_COLOR forState:UIControlStateNormal];
        _commintBt.titleLabel.font=FONT_16;
        [_blackView addSubview:_commintBt];

        
        
        
    }
    return self;
}

- (void)doLogin
{
    if (_textField1.text.length==0) {
        [Commend showAlert:@"请输入用户名"];
        return;
    }
    else if (_textField2.text.length==0)
    {
        [Commend showAlert:@"请输入密码"];
        return;
    }
    else
    {
        if ([_delegate respondsToSelector:@selector(doLoginMethod:andPassword:)]) {
            [_delegate doLoginMethod:_textField1.text andPassword:_textField2.text];
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _oquen.frame=CGRectMake(0, 0, self.width, self.height);
    _blackView.frame=CGRectMake(RESIZE_UI(20), (self.height-RESIZE_UI(240))/2-RESIZE_UI(20), self.width-RESIZE_UI(40), RESIZE_UI(240));
    _topView.frame=CGRectMake(0, 0, _blackView.width, RESIZE_UI(40));
    _topLabel.frame=CGRectMake(RESIZE_UI(5), 0, _topView.width-RESIZE_UI(10), _topView.height);
    _textField1.frame=CGRectMake(RESIZE_UI(5), _topView.bottom+RESIZE_UI(20), _blackView.width-RESIZE_UI(10), RESIZE_UI(40));
    _line1.frame=CGRectMake(_textField1.left, _textField1.bottom+RESIZE_UI(2), _textField1.width, 1.5);
    _textField2.frame=CGRectMake(_textField1.left, _line1.bottom+RESIZE_UI(10), _textField1.width, _textField1.height);
    
    _line2.frame=CGRectMake(_textField2.left, _textField2.bottom+RESIZE_UI(2), _textField2.width, 1.5);
    
    _cancelBt.frame=CGRectMake(_blackView.width/2+RESIZE_UI(5), _line2.bottom+RESIZE_UI(20), RESIZE_UI(50), RESIZE_UI(40));
    _commintBt.frame=CGRectMake(_cancelBt.right+RESIZE_UI(10), _cancelBt.top, _cancelBt.width, _cancelBt.height);
    
}

- (void)show
{
    _textField1.text = @"";
    _textField2.text = @"";
    [UIView animateWithDuration:0.3 animations:^{
        _oquen.alpha=1;
        self.alpha=1;
        
    }];
}
- (void)hide
{
    [_textField1 resignFirstResponder];
    [_textField2 resignFirstResponder];
    [UIView animateWithDuration:0.3 animations:^{
        _oquen.alpha=0;
        self.alpha=0;
        
    }];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
